#include "LedCnt.h"
#include <cstring>
#include "log.h"

extern "C" int MEDIA_TailMatchLed(){
    LedColor_t color;
    color.r = 0;
    color.g = 0;
    color.b = 255;
    if(LedCnt::instance()->setStaticLed(color)){
        return 0;
    }
    return -1;
}

extern "C" int MEDIA_TailMatchingLed(){
    LedColor_t color;
    color.r = 0;
    color.g = 0;
    color.b = 255;
    if(LedCnt::instance()->setFlickLed(color, 1000, 1000)){
        return 0;
    }
    return -1;
}


LedCnt* LedCnt::sm_ledCnt = nullptr;
mutex   LedCnt::sm_instanceLock;

LedCnt* LedCnt::instance(){
    if(sm_ledCnt == nullptr){
        lock_guard<mutex> lock(sm_instanceLock);
        sm_ledCnt = new LedCnt();
    }
    return sm_ledCnt;
}


bool LedCnt::setStaticLed(LedColor_t color){
    LedState_t ledState;
	memset(&ledState, 0, sizeof(ledState));
	ledState.ledId = 0;
    ledState.mode = 1;
    ledState.periodCnt = -1;
    ledState.strength = 300;

	ledState.onColor = color;
    		
	if(SysLedSet(ledState)){
		return false;
	}
	return true;
}
	
bool LedCnt::setFlickLed(LedColor_t color, int onTime, int offTime){
    LedState_t ledState;
	memset(&ledState, 0, sizeof(ledState));
	ledState.ledId = 0;
	ledState.periodCnt = -1;
	ledState.strength = 300;
	ledState.mode = 2;

	ledState.onCycle = onTime;
	ledState.offCycle = offTime;

	ledState.onColor = color;

	ledState.offColor.r = 0;
	ledState.offColor.g = 0;
	ledState.offColor.b = 0;
				
	if(SysLedSet(ledState)){
		return false;
	}
	return true;
}

